package com.xinlin.common.utils;

/**
 *浮点型数据进行计算和比较的工具类
 * @author  xiongbiao
 * @version  1.0, 2021年7月17日
 * @see  NumberUtils
 * @since  银行级别的工具类
 */

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberUtil {
	/**
	 *
	 * 浮点型数据相加
	 *
	 * @param v1
	 * @param v2
	 * @return
	 * @see NumberUtil#(double, double)
	 */
	public static double add(Object v1, Object v2) {
		if (ObjectUtils.isEmpty(v1)) {
			v1 = BigDecimal.ZERO;
		}
		if (ObjectUtils.isEmpty(v2)) {
			v2 = BigDecimal.ZERO;
		}
		BigDecimal b1 = new BigDecimal(v1.toString());
		BigDecimal b2 = new BigDecimal(v2.toString());
		return b1.add(b2).doubleValue();
	}

	/**
	 *
	 * 浮点型数据相减
	 *
	 * @param v1
	 * @param v2
	 * @return
	 * @see NumberUtil#(double, double)
	 */
	public static double sub(Object v1, Object v2) {
		if (ObjectUtils.isEmpty(v1)) {
			v1 = BigDecimal.ZERO;
		}
		if (ObjectUtils.isEmpty(v2)) {
			v2 = BigDecimal.ZERO;
		}
		BigDecimal b1 = new BigDecimal(v1.toString());
		BigDecimal b2 = new BigDecimal(v2.toString());
		return b1.subtract(b2).doubleValue();
	}

	/**
	 * 判断两个浮点型是否相等
	 * @param v1
	 * @param v2
	 * @return
	 */
	public static boolean equal(double v1, double v2){
		return compare(v1,v2)==0;
	}

	/**
	 *
	 * 对比数字大小
	 *
	 * @param
	 * @return
	 * @see NumberUtil#isNumber(String)
	 */
	public static int compare(double v1, double v2) {
		int result = 0;
		BigDecimal b1 = new BigDecimal(Double.toString(v1));
		BigDecimal b2 = new BigDecimal(Double.toString(v2));
		// b1小于b2
		if (b1.compareTo(b2) == -1) {
			result = -1;
		}
		// b1大于b2
		if (b1.compareTo(b2) == 1) {
			result = 1;
		}
		return result;
	}

	/**
	 * BigDecimal -> double
	 * @param value
	 * @return
	 */
	public static double bigDecimaToDouble(BigDecimal value){
		if (ObjectUtils.isEmpty(value)) {
			value = BigDecimal.ZERO;
		}
		return value.doubleValue();
	}



	/**
	 *
	 * 浮点型数据相乘
	 *
	 * @param v1
	 * @param v2
	 * @return
	 * @see NumberUtil#mul(double, double)
	 */
	public static double mul(double v1, double v2) {
		BigDecimal b1 = new BigDecimal(Double.toString(v1));
		BigDecimal b2 = new BigDecimal(Double.toString(v2));
		return b1.multiply(b2).doubleValue();
	}

	/**
	 *
	 * 浮点型数据相除
	 *
	 * @param v1
	 * @param v2
	 * @return
	 * @see NumberUtil#div(double, double)
	 */
	public static double div(double v1, double v2) {
		return div(v1, v2, 3);
	}

	/**
	 *
	 * 浮点型数据相除
	 *
	 * @param v1
	 * @param v2
	 * @param count
	 *            精度
	 * @return
	 * @see NumberUtil#div(double, double, int)
	 */
	public static double div(double v1, double v2, int count) {
		BigDecimal b1 = new BigDecimal(Double.toString(v1));
		BigDecimal b2 = new BigDecimal(Double.toString(v2));
		return b1.divide(b2, count, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

	/**
	 *
	 * 截取3位
	 *
	 * @param v
	 *            浮点数
	 * @param count
	 *            截取的位数
	 * @return
	 * @see NumberUtil#round(double, int)
	 */
	public static double round(double v, int count) {
		BigDecimal b = new BigDecimal(Double.toString(v));
		BigDecimal one = new BigDecimal("1");
		return b.divide(one, count, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

	/**
	 *
	 * 格式化数据
	 *
	 * @param pattern
	 *            数据格式‘0.00’
	 * @param value
	 *            截取的位数
	 * @return
	 * @see NumberUtil#(String, double)
	 */
	public static String decimalFormat(String pattern, double value) {
		return new DecimalFormat(pattern).format(value);
	}

	/**
	 *
	 * 校验是否是数字
	 *
	 * @param value
	 * @return
	 * @see NumberUtil#isNumber(String)
	 */
	public static boolean isNumber(String value) {
		String patternStr = "^\\d+$";
		// 忽略大小写
		Pattern p = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(value);
		return m.find();
	}

	/**
	 * KG=>磅
	 *
	 * @param kiloGram
	 * @return
	 */
	public static Long kiloGramToPound(Object kiloGram) {
		if (kiloGram == null) {
			return null;
		}
		String value = decimalFormat("0",
				mul(Double.parseDouble(kiloGram.toString()), 2.2046D));
		return Long.parseLong(value);
	}

	/**
	 * 磅=>KG
	 *
	 * @param
	 * @return
	 */
	public static Long poundToKiloGram(Object pound) {
		if (pound == null) {
			return null;
		}
		String value = decimalFormat("0",
				div(Double.parseDouble(pound.toString()), 2.2046D));
		return Long.parseLong(value);
	}

	/**
	 * 保留一位小数
	 *
	 * @param
	 * @return
	 */
	public static String aDecimal(Object oldValue) {
		if (oldValue == null) {
			return null;
		}
		String value = decimalFormat("0.0",
				Double.parseDouble(oldValue.toString())+0.001);
		return value;
	}

	/**
	 * 保留两位小数
	 *
	 * @param
	 * @return
	 */
	public static String twoDecimal(Object oldValue) {
		if (oldValue == null) {
			return null;
		}
		String value = decimalFormat("0.00",
				Double.parseDouble(oldValue.toString())+0.0001);
		return value;
	}

	/**
	 * 取整
	 *
	 * @param
	 * @return
	 */
	public static String convertInteger(Object oldValue) {
		if (oldValue == null) {
			return null;
		}
		String value = decimalFormat("0",
				Double.parseDouble(oldValue.toString()));
		return value;
	}

	public static void main(String[] args) {

	}
}